package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.ProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品单位 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@RestController
@RequestMapping("/productUnit")
public class ProductUnitController extends BaseController {

    @Autowired
    private ProductUnitService productUnitService;
    @Operation(summary = "查所有单位")
    @GetMapping("list")
    public TableDataInfo findpage(@RequestParam(value = "name",required = false) String name){
        //先分页
        startPage();
        List<ProductUnit> list = productUnitService.slecetproductunit(name);
        return getDataTable(list);

    }
    @Operation(summary = "新增单位")
    @PostMapping()
    public AjaxResult add(@RequestBody ProductUnit productUnit){
        productUnit.setCreateBy(SecurityUtils.getUsername());
        productUnitService.save(productUnit);
        return AjaxResult.success();
    }
    @Operation(summary = "获取单位信息")
    @GetMapping("{id}")
    public AjaxResult getdate(@PathVariable("id")Long id){
      return AjaxResult.success(productUnitService.getById(id));
    }
    @Operation(summary = "修改单位")
    @PutMapping()
    public AjaxResult updatedate(@RequestBody ProductUnit productUnit){
        productUnit.setCreateBy(SecurityUtils.getUsername());
        productUnitService.updateById(productUnit);
        return AjaxResult.success();
    }
    @Operation(summary = "删除单位")
    @DeleteMapping("{id}")
    public AjaxResult deleltdate(@PathVariable("id") Long id){
        productUnitService.removeById(id);
        return AjaxResult.success();
    }
    @Operation(summary = "在新增商品页面查所有单位")
    @GetMapping("getUnitAll")
    public AjaxResult getunit(){
        List<ProductUnit> list= productUnitService.list();
        return AjaxResult.success(list);
    }
}
